package com.situ.hospital.dao.Impl;

import com.situ.hospital.dao.IRestockingDAO;
import com.situ.hospital.pojo.Restocking;
import com.situ.hospital.pojo.query.RestockingQuery;
import com.situ.hospital.pojo.vo.RestockingVO;
import com.situ.hospital.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class RestockingDAOImpl implements IRestockingDAO {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<RestockingVO> selectByPage(RestockingQuery restockingQuery) {
        String sql = "SELECT r.id,m.`name` AS medicineName,r.purchase_price,r.quantity,r.create_time,r.update_time" +
                " FROM restocking AS r LEFT JOIN medicine AS m ON r.medicine_id=m.id WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        Integer queryMedicineId = restockingQuery.getMedicineId();
        if(queryMedicineId != null) {
            sql += " AND r.medicine_id=?";
            queryList.add(queryMedicineId);
        }
        Double leastPurchasePrice = restockingQuery.getLeastPurchasePrice();
        Double mostPurchasePrice = restockingQuery.getMostPurchasePrice();
        if(leastPurchasePrice != null && mostPurchasePrice != null) {
            sql += " AND r.purchase_price BETWEEN ? AND ?";
            queryList.add(leastPurchasePrice);
            queryList.add(mostPurchasePrice);
        }
        Integer leastQuantity = restockingQuery.getLeastQuantity();
        Integer mostQuantity = restockingQuery.getMostQuantity();
        if(leastQuantity != null && mostQuantity != null) {
            sql += " AND r.quantity BETWEEN ? AND ?";
            queryList.add(leastQuantity);
            queryList.add(mostQuantity);
        }
        sql += " LIMIT ?,?";
        int offset = (restockingQuery.getPage() - 1) * restockingQuery.getLimit();
        queryList.add(offset);
        queryList.add(restockingQuery.getLimit());
        return template.query(sql, new BeanPropertyRowMapper<>(RestockingVO.class), queryList.toArray());
    }

    @Override
    public int selectTotalCount(RestockingQuery restockingQuery) {
        String sql = "SELECT COUNT(*) FROM restocking WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        Integer queryMedicineId = restockingQuery.getMedicineId();
        if(queryMedicineId != null) {
            sql += " AND medicine_id=?";
            queryList.add(queryMedicineId);
        }
        Double leastPurchasePrice = restockingQuery.getLeastPurchasePrice();
        Double mostPurchasePrice = restockingQuery.getMostPurchasePrice();
        if(leastPurchasePrice != null && mostPurchasePrice != null) {
            sql += " AND purchase_price BETWEEN ? AND ?";
            queryList.add(leastPurchasePrice);
            queryList.add(mostPurchasePrice);
        }
        Integer leastQuantity = restockingQuery.getLeastQuantity();
        Integer mostQuantity = restockingQuery.getMostQuantity();
        if(leastQuantity != null && mostQuantity != null) {
            sql += " AND quantity BETWEEN ? AND ?";
            queryList.add(leastQuantity);
            queryList.add(mostQuantity);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        String sql = "DELETE FROM restocking WHERE id=?";
        template.update(sql, id);
    }

    @Override
    public void add(Restocking restocking) {
        String sql = "INSERT INTO restocking(medicine_id,purchase_price,quantity) VALUES(?,?,?)";
        template.update(sql, restocking.getMedicineId(), restocking.getPurchasePrice(), restocking.getQuantity());
    }

    @Override
    public Restocking selectById(int id) {
        String sql = "SELECT medicine_id,purchase_price,quantity,create_time,update_time FROM restocking WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Restocking.class), id);
    }

    @Override
    public void update(Restocking restocking) {
        String sql = "UPDATE restocking SET medicine_id=?,purchase_price=?,quantity=? WHERE id=?";
        template.update(sql, restocking.getMedicineId(), restocking.getPurchasePrice(), restocking.getQuantity(), restocking.getId());
    }

    @Override
    public Restocking selectByMedicineId(int id) {
        String sql = "SELECT medicine_id,purchase_price,quantity,create_time,update_time FROM restocking WHERE medicine_id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Restocking.class), id);
    }
}
